// ignore_for_file: no_logic_in_create_state

/*
 * @Author: Eureka 
 * @Date: 2019-09-18 16:08:57 
 * @Last Modified by: Eureka
 * @Last Modified time: 2022-01-19 18:49:48
 * BaseWidget 页面基础widget
 */

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:senkoo_platform/base_navigation_manager/base_navigation_manager.dart';
import 'package:senkoo_platform/base_function/base_function.dart';
import 'package:shared_preferences/shared_preferences.dart';

abstract class BaseWidget extends StatefulWidget {
  late final BaseWidgetState baseWidgetState;

  @override
  BaseWidgetState createState() {
    baseWidgetState = getState();
    return baseWidgetState;
  }

  BaseWidgetState getState();
  String? getStateName() {
    return baseWidgetState.getClassName();
  }
}

abstract class BaseWidgetState<T extends BaseWidget> extends State<T>
    with WidgetsBindingObserver, AutomaticKeepAliveClientMixin
    implements BaseFuntion {
  bool _onResumed = false; //页面展示标记
  bool _onPause = false; //页面暂停标记
  String themeColor = '';
  DateTime? lastPopTime;

  // Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    initBaseCommon(this, context);
    WidgetsBinding.instance.addObserver(this);
    NavigationManager().addWidget(this);
    onCreate();
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await SharedPreferences.getInstance();
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void deactivate() {
    //说明是被覆盖了
    if (NavigationManager().isSecondTop(this)) {
      if (!_onPause) {
        onPause();
        _onPause = true;
      } else {
        onResume();
        _onPause = false;
      }
    } else if (NavigationManager().isTopPage(this)) {
      if (!_onPause) {
        onPause();
      }
    }
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (!_onResumed) {
      //说明是 初次加载
      if (NavigationManager().isTopPage(this)) {
        // FlutterUmengPlugin.beginPageView(getClassName());
        _onResumed = true;
        onResume();
      }
    }

    return Scaffold(
      // drawerEnableOpenDragGesture: false,
      key: scaffoldKey,
      drawer: prohibitDrawerBool
          ? SizedBox(
              width: 320.w,
              child: Drawer(
                child: getDrawer(),
              ),
            )
          : null,
      body:
          //  BuildConfig.isAndroid
          //     ? WillPopScope(
          //         onWillPop: () async {
          //           if (lastPopTime == null || DateTime.now().difference(lastPopTime!) > const Duration(seconds: 2)) {
          //             lastPopTime = DateTime.now();
          //             showToast('再按一次返回退出'); // 提示消息，可以是其他的Toast
          //           } else {
          //             lastPopTime = DateTime.now();
          //             await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          //           }
          //           return false;
          //         },
          //         child: SingleChildScrollView(
          //           physics: const NeverScrollableScrollPhysics(),
          //           controller: ScrollController(),
          //           child: getBaseView(context),
          //         ),
          //       )
          //     :
          SingleChildScrollView(
        physics: const NeverScrollableScrollPhysics(),
        controller: ScrollController(),
        child: getBaseView(context),
      ),
    );
  }

  @override
  void dispose() {
    onDestory();
    WidgetsBinding.instance.removeObserver(this);
    _onResumed = false;
    _onPause = false;

    //把改页面 从 页面列表中 去除
    NavigationManager().removeWidget(this);
    //取消网络请求
    // HttpManager.cancelHttp(getClassName());
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    //此处可以拓展 是不是从前台回到后台
    if (state == AppLifecycleState.resumed) {
      //on resume
      if (NavigationManager().isTopPage(this)) {
        onForeground();
        onResume();
      }
    } else if (state == AppLifecycleState.paused) {
      //onpause
      if (NavigationManager().isTopPage(this)) {
        onBackground();
        onPause();
      }
    }
    super.didChangeAppLifecycleState(state);
  }

  @override
  bool get wantKeepAlive => true;
}
